import {timeFormat} from "@/utils/util";
import {UnwrapNestedRefs} from "@vue/reactivity";
import {AfterSaleConfigItemDTO} from "@/api/system/dto/ AfterSaleConfigItemDTO";
import {AfterSaleEnumConfigManager} from "@/manager/base/AfterSaleEnumManager";
import {AfterSaleRejectTypeEnumList} from "@/config/enums/aftersale/AfterSaleRejectTypeEnum";
import {AfterSaleSendTypeEnumEnumList} from "@/config/enums/aftersale/AfterSaleSendTypeEnum";
import {AfterSaleWarehouseConfigEnum} from "@/config/enums/aftersale/AfterSaleWarehouseEnum";

/**
 * 时间 店铺 渠道 等映射
 * 处理器
 *
 * @author 徐寿春
 * 2023/4/11 13:46
 */
export function useFormatterTimeManager() {
    /**
     *  转化时间
     * @author 徐寿春
     * 2023/4/8 13:24
     */
    function formatterCreateTime(row: any, column: any) {
        return timeFormat(row.createTime)
    }


    /**
     *  转化时间
     * @author 徐寿春
     * 2023/4/8 13:24
     */
    function formatterUpdateTime(row: any, column: any) {
        return timeFormat(row.updateTime)
    }

    return {formatterCreateTime, formatterUpdateTime}

}


/**
 *
 * 转化管理器
 *
 * @author 徐寿春
 * 2023/4/12 08:33
 */
export function useFormatterManager(afterSaleEnumManager: UnwrapNestedRefs<AfterSaleEnumConfigManager>) {

    /**
     *  转化时间
     * @author 徐寿春
     * 2023/4/8 13:24
     */
    function formatterCreateTime(row: any, column: any) {
        return timeFormat(row.createTime)
    }


    /**
     *  转化时间
     * @author 徐寿春
     * 2023/4/8 13:24
     */
    function formatterUpdateTime(row: any, column: any) {
        return timeFormat(row.updateTime)
    }


    /**
     * 渠道
     *
     * @author 徐寿春
     * 2023/4/8 13:23
     */
    function formatterChannel(row: any, column: any) {
        let currentValue = ''
        switch (column.property) {
            case 'afterSaleOrderDetail.channel':
                currentValue = row.afterSaleOrderDetail.channel
                break
            case 'channel':
                currentValue = row.channel
                break
        }

        const result = afterSaleEnumManager.orderChannelConfig.find((item) => {
            return item.name == currentValue
        });

        if (!!result) {
            return result.value
        } else {
            return row.channel
        }


    }

    /**
     *  类型
     * @author 徐寿春
     * 2023/4/8 13:23
     */
    function formatterOrderType(row: any, column: any) {


        let currentValue = ''
        switch (column.property) {
            case 'afterSaleOrderDetail.afterSaleOrderType':
                currentValue = row.afterSaleOrderDetail.afterSaleOrderType
                break
            case 'afterSaleOrderType':
                currentValue = row.afterSaleOrderType
                break
        }


        const result = afterSaleEnumManager.afterSaleOrderType.find((item) => {
            return item.code == currentValue

        });
        if (!!result) {
            return result.name
        } else {
            return currentValue
        }

    }

    /**
     * 来源
     *
     * @author 徐寿春
     * 2023/4/8 13:23
     */
    function formatterReSource(row: any, column: any) {

        let currentValue = ''
        switch (column.property) {
            case 'afterSaleOrderDetail.source':
                currentValue = row.afterSaleOrderDetail.source
                break
            case 'source':
                currentValue = row.source
                break
        }


        const result = afterSaleEnumManager.orderSourceConfig.find((item) => {
            return item.name == currentValue

        });
        if (!!result) {
            return result.value
        } else {
            return currentValue
        }

    }

    /**
     * 店铺
     *
     * @param row
     * @param column
     */
    function formatterShop(row: any, column: any) {


        switch (column.property) {
            case 'afterSaleOrderDetail.shopName':

                const results = afterSaleEnumManager.orderChannelConfig.find((item) => {
                    return item.name == row.afterSaleOrderDetail.channel
                });

                if (!!results) {
                    const shopResult = results.children.find(item => {
                        return item.name == row.afterSaleOrderDetail.shopName

                    })
                    if (!!shopResult) {
                        return shopResult.value
                    } else {
                        return row.afterSaleOrderDetail.shopName

                    }
                }

                break
            case 'shopName':
                row.shopName
                // 找到渠道
                const result = afterSaleEnumManager.orderChannelConfig.find((item) => {
                    return item.name == row.channel
                });


                if (!!result) {
                    const shopResult = result.children.find(item => {
                        return item.name == row.shopName

                    })
                    if (!!shopResult) {
                        return shopResult.value
                    } else {
                        return row.shopName
                    }
                }


                break
        }


    }


    /**
     * 计算店铺
     *
     * @author 徐寿春
     * 2023/4/12 08:00
     */
    function computeFormatterShop(channel?: any): AfterSaleConfigItemDTO[] {

        if (!!channel) {
            const result = afterSaleEnumManager.orderChannelConfig.find((item) => {
                return item.name == channel
            });

            if (!!result) {
                return result.children
            }

        }

        let arr: AfterSaleConfigItemDTO[] = []
        afterSaleEnumManager.orderChannelConfig.filter(item => item.children).forEach(item => {
            arr.push(...item.children)
        })

        return arr


    }

    /**
     * 计算店铺
     *
     * @author 徐寿春
     * 2023/4/12 08:00
     */
    function computeFormatterChannel(shopName: any) {

        if (!!shopName) {
            let shopList = computeFormatterShop();

            const result = shopList.find((item) => {
                return item.name == shopName
            });

            if (!!result) {
                return afterSaleEnumManager.orderChannelConfig.filter(item => {
                    return item.id == result.pid
                })


            }
        } else {
            return afterSaleEnumManager.orderChannelConfig
        }

    }


    /**
     * 计算店铺仓库
     *
     * @author 徐寿春
     * 2023/4/12 08:00
     */
    function computeFormatterWarehouse(name: any) {
        let arr: AfterSaleConfigItemDTO[] = []
        afterSaleEnumManager.materialSituationConfig.filter(item => item.children).map(item => arr.push(...item.children));
        return arr.filter(item => item.name == name)

    }


    function computeFormatterFailCause(name: any) {
        let arr: AfterSaleConfigItemDTO[] = []
        afterSaleEnumManager.materialFailureCauseConfig.filter(item => item.children).map(item => arr.push(...item.children));
        return arr.filter(item => item.name == name)

    }


    /**
     * 映射协同类型处理
     *
     * @author 徐寿春
     * 2023/4/14 08:57
     */
    function formatterCooperateType(row: any, column: any) {
        let find = afterSaleEnumManager.cooperateOrderType.find(item => item.code === row.type);
        if (!!find) {
            return find.name;
        } else {
            row.type
        }

    }

    /**
     * 仓库映射
     *
     * @author 徐寿春
     * 2023/4/14 08:57
     */
    function formatterWarehouse(row: any, column?: any) {
        let find = AfterSaleWarehouseConfigEnum.find(item => item.code === row.warehouse)
        if (!!find) {
            return find.name;
        } else {
            row.warehouse
        }
    }


    /**
     * 映射解决方式
     *
     * @param row
     * @param column
     */
    function formatterResolveWay(row: any, column?: any) {
        let find = afterSaleEnumManager.cooperateProcessMode
            .find(item => item.value === row.inMaterIalInfo?.checkFault)?.children?.find(item => item.name == row.inMaterIalInfo.resolveWay)

        if (!!find) {
            return find.value
        }

        return row.inMaterIalInfo.resolveWay
    }


    /**
     * 映射发运单类型
     *
     * @param row
     * @param column
     */
    function formatterShippingType(row: any, column?: any) {
        AfterSaleSendTypeEnumEnumList.forEach(item => AfterSaleRejectTypeEnumList.push(item))
        let find = AfterSaleRejectTypeEnumList.find(item => item.code == row.type);
        if (!!find) {
            return find.name
        }
        return row.type
    }


    /**
     * 映射仓库配置信息，公司，仓位
     *
     * @param row
     * @param column
     */
    function formatterWarehouseConfig(row: any, column?: any) {

        if (column.property == "company") {
            const find = afterSaleEnumManager.orderWarehouseConfig.find(item => item.name == row.company);
            if (!!find) {
                return find.value
            }
            return row.company
        }


        if (column.property == "warehouse") {
            const find = afterSaleEnumManager.orderWarehouseConfig.find(item => item.name == row.company);
            if (!!find) {
                const find1 = find.children.find(item => item.name == row.warehouse);
                if (!!find1) {
                    return find1.value
                }
            }

            return row.warehouse
        }


    }

    return {
        formatterWarehouseConfig,
        formatterShippingType,
        formatterResolveWay,
        formatterWarehouse,
        formatterCooperateType,
        computeFormatterFailCause,
        computeFormatterWarehouse,
        computeFormatterChannel,
        computeFormatterShop,
        formatterUpdateTime,
        formatterCreateTime,
        formatterChannel,
        formatterReSource,
        formatterOrderType,
        formatterShop
    }


}






